home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / lineno / editdemo.frm (.txt) next >
Encoding:
Visual Basic Form  |  1995-05-07  |  3.5 KB  |  108 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   4020
  5.    ClientLeft      =   225
  6.    ClientTop       =   1485
  7.    ClientWidth     =   6630
  8.    Height          =   4425
  9.    Left            =   165
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4020
  12.    ScaleWidth      =   6630
  13.    Top             =   1140
  14.    Width           =   6750
  15.    Begin Timer Timer1 
  16.       Interval        =   100
  17.       Left            =   3720
  18.       Top             =   120
  19.    End
  20.    Begin TextBox Text2 
  21.       Height          =   285
  22.       Left            =   4440
  23.       TabIndex        =   2
  24.       Text            =   "Click to change focus."
  25.       Top             =   1560
  26.       Width           =   2055
  27.    End
  28.    Begin CommandButton Command1 
  29.       Caption         =   "Exit"
  30.       Height          =   375
  31.       Left            =   4440
  32.       TabIndex        =   1
  33.       Top             =   1080
  34.       Width           =   1095
  35.    End
  36.    Begin TextBox Text1 
  37.       Height          =   3015
  38.       Left            =   480
  39.       MultiLine       =   -1  'True
  40.       TabIndex        =   0
  41.       Text            =   "Text1"
  42.       Top             =   840
  43.       Width           =   3855
  44.    End
  45.    Begin Label Label5 
  46.       Height          =   255
  47.       Left            =   1440
  48.       TabIndex        =   6
  49.       Top             =   480
  50.       Width           =   495
  51.    End
  52.    Begin Label Label4 
  53.       Caption         =   "Line No.:"
  54.       Height          =   255
  55.       Left            =   480
  56.       TabIndex        =   5
  57.       Top             =   480
  58.       Width           =   855
  59.    End
  60.    Begin Label Label3 
  61.       Caption         =   "Character:"
  62.       Height          =   255
  63.       Left            =   480
  64.       TabIndex        =   4
  65.       Top             =   120
  66.       Width           =   855
  67.    End
  68.    Begin Label Label2 
  69.       Height          =   255
  70.       Left            =   1440
  71.       TabIndex        =   3
  72.       Top             =   120
  73.       Width           =   615
  74.    End
  75. 'API function declarations needed to get line number
  76. Declare Function GetFocus Lib "user" () As Integer
  77. Declare Function SendMessage Lib "user" (ByVal a%, ByVal b%, ByVal c%, ByVal d As Any) As Long
  78. Sub Command1_Click ()
  79. End Sub
  80. Sub Form_Load ()
  81. thetext$ = "This is a demonstration of getting the current cursor position and line number from a text box control. "
  82. thetext$ = thetext$ + "Formerly, in VB 1.0, it was almost impossible to always know which line the cursor was on when "
  83. thetext$ = thetext$ + "focus switched from another control to the text box. To see how this works, click on the box to "
  84. thetext$ = thetext$ + "the right, then click again, anywhere inside this text box, or type in the box."
  85. text1.Text = thetext$
  86. End Sub
  87. Function GetLineNo (charpos&)
  88. 'Set constant value
  89. Const EM_linefromchar = &H400 + 25
  90. 'Get current line number from cursor position
  91. pos% = 1 + SendMessage(GetFocus(), EM_linefromchar, -1, 0&)
  92. 'Assign return value to function
  93. GetLineNo = pos%
  94. End Function
  95. Sub Text1_GotFocus ()
  96. timer1.interval = 100 'Sets timer interval to .1 sec.
  97. timer1.Enabled = True 'activates timer routine to get line number when edit control has focus.
  98. End Sub
  99. Sub Text1_LostFocus ()
  100. timer1.Enabled = False 'disables timer when focus is lost.
  101. End Sub
  102. Sub Timer1_Timer ()
  103. label2.Caption = text1.SelStart 'gets character position
  104. pos& = text1.SelStart    'assign to variable
  105. currline = GetLineNo(pos&) 'calls linenumber function
  106. label5.Caption = currline 'displays current line number
  107. End Sub
  108.